package com.bby.disk.exception;

import com.bby.common.exception.BBYException;
import com.bby.common.utils.R;
import com.bby.common.utils.StatusCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class BBYExceptionHandler {
    public static final BBYException INPUT_ERROR_EX = new BBYException(StatusCode.INPUT_ERROR,"提供的参数不符合规范!");
    public static final BBYException NON_AUTHORITY_EX = new BBYException(StatusCode.NON_AUTHORITY,"没有访问权限！");
    public static final BBYException REDIS_ERROR_EX = new BBYException(StatusCode.UNKNOWN_ERROR,"redis错误，请联系管理员！");
    public static final BBYException FILE_NOT_FOUNT = new BBYException(StatusCode.NOT_FOUND,"文件不存在！");
    Logger logger = LoggerFactory.getLogger(this.getClass());
    /**
     * 全局异常捕捉处理
     * @param ex
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public R systemErrorHandler(Exception ex) {
        logger.error("未处理的异常",ex);
        return R.error();
    }
    /**
     * 全局异常捕捉处理
     * @param ex
     */
    @ResponseBody
    @ExceptionHandler(value = BBYException.class)
    public R bbyErrorHandler(BBYException ex) {
        logger.error("自定义异常:code={},msg={}",ex.getCode(),ex.getMessage());
        return R.error(ex.code,ex.getMessage());
    }
}
